package webshop.controller.action;

import java.util.ArrayList;
import java.util.List;

import webshop.model.bl.CategoryManager;
import webshop.model.bl.ProductManager;
import webshop.model.db.Category;

import com.opensymphony.xwork2.ActionSupport;

public class ProductSearchAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8055917040977630187L;
	private List<Category> categoryList = new ArrayList<Category>();
	private List<Category> resultList = new ArrayList<Category>();
	private boolean searchDataSend = false;
	private String searchIn;
	private int categoryId;
	private String searchFor;
	
	public String execute() {
		categoryList = new CategoryManager().getAllCategories();
		System.out.println("executing");
		if (searchDataSend) {
			if ("".equals(searchFor)) {
				addFieldError("searchFor", "");
				addActionError(getText("error.empty.fields"));
				return SUCCESS;
			}
			
			resultList = new ProductManager().searchForProduct(searchFor, categoryId, searchIn);
			if (resultList == null || resultList.size() <= 0) {
				addActionMessage(getText("message.noresults"));
			}
		}
		return SUCCESS;
	}
	
	public List<String> getSearchCriteria() {
		List<String> searchCriteria = new ArrayList<String>();
		searchCriteria.add("Beschreibung");
		searchCriteria.add("Name");
		return searchCriteria;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	public boolean isSearchDataSend() {
		return searchDataSend;
	}

	public void setSearchDataSend(boolean searchDataSend) {
		this.searchDataSend = searchDataSend;
	}

	public void setSearchIn(String searchIn) {
		this.searchIn = searchIn;
	}

	public void setCategoryId(int categoryId) {
		this.categoryId = categoryId;
	}

	public void setSearchFor(String searchFor) {
		this.searchFor = searchFor;
	}

	public List<Category> getResultList() {
		return resultList;
	}

	public void setResultList(List<Category> resultList) {
		this.resultList = resultList;
	}

}
